package com.tsunami.auth.service.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsunami.auth.mapper.ServerMapper;
import com.tsunami.auth.service.auth.IClientsService;
import com.tsunami.auth.service.auth.IServerService;
import com.tsunami.authority.dto.auth.ServerSaveDTO;
import com.tsunami.authority.dto.auth.ServerUpdateDTO;
import com.tsunami.authority.entity.auth.Clients;
import com.tsunami.authority.entity.auth.Server;
import com.tsunami.base.AppUtils;
import com.tsunami.base.R;
import com.tsunami.exception.BizException;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.user.model.SysUser;
import com.tsunami.utils.PageUtils;
import com.tsunami.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务管理 服务实现类
 * </p>
 *
 * @author lx
 * @since 2021-11-29
 */
@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> implements IServerService {

    @Autowired
    IClientsService clientsService;

    @Override
    public R selectPageList(Server data) {
        Page<Server> pages = baseMapper.selectPageList(PageUtils.buildPage(),data);
        return R.ok().data(pages);
    }

    @Override
    public Server saveServer(SysUser user,ServerSaveDTO data) {
        Server server = new Server();
        server.setServerName(data.getServerName());
        server.setServerCode(data.getServerCode());
        server.setClientId(AppUtils.getAppId());
        server.setCreateUser(user.getId());
        server.setUpdateUser(user.getId());
        super.save(server);
        Clients clients = new Clients();
        clients.setClientId(server.getClientId());
        clientsService.save(clients);
        return server;
    }

    @Override
    public boolean isExist(String serverCode) {
        QueryWrapper param = new QueryWrapper();
        param.eq("server_code",serverCode);
        Server server = getOne(param);
        if(server!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean remove(List<Long> ids) {
        if(ids!=null&&ids.size()>0){
            for(Long id:ids){
                Server server = getById(id);
                QueryWrapper param = new QueryWrapper();
                param.eq("server_code",server.getServerCode());
                Clients clients = clientsService.getOne(param);
                if(clients!=null){
                    clientsService.removeClient(clients);
                }
            }
        }
        return super.removeByIds(ids);
    }

    @Override
    public Server updateUser(SysUser user, ServerUpdateDTO data) {
        Server server = getById(data.getId());
        if(StringUtils.isNotEmpty(data.getServerCode())){
            QueryWrapper param = new QueryWrapper();
            param.eq("server_code",data.getServerCode());
            param.eq("id",data.getId());
            if(this.count(param)>0&&server.getId()!=data.getId()){
                throw new BizException(ExceptionCode.HAVE_CODE);
            }
        }
        server.setUpdateUser(user.getId());
        server.setServerName(data.getServerName());
        server.setServerCode(data.getServerCode());
        super.updateById(server);
        return server;
    }

    @Override
    public boolean checkCodeUnique(String code) {
        QueryWrapper param = new QueryWrapper();
        param.eq("server_code",code);
        int count = baseMapper.selectCount(param);
        if(count>0){
            return true;
        }
        return false;
    }
}
